<?php

namespace addons\groupon\model\store;

use app\admin\model\Admin;
use think\Model;
use addons\groupon\model\User;
use addons\groupon\model\Area;
use addons\groupon\exception\Exception;

/**
 * 自提点申请
 */
class Apply extends Model
{
    // 表名,不含前缀
    protected $name = 'groupon_store_apply';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $hidden = [];

    protected $append = [
        'status_name',
        'images_original'
    ];


    /**
     * 自提点申请详情
     *
     * @return object
     */
    public static function info()
    {
        $user = User::info();

        // 查询审核未通过的或者驳回的自提点申请信息
        $apply = self::where('user_id', $user->id)->where('status', 'in', [-1, 0])->find();

        return $apply;
    }


    /**
     * 自提点申请
     */
    public static function apply($params)
    {
        $user = User::info();

        extract($params);

        $area       = Area::get($area_id);
        $city       = Area::get($area->pid);
        $province   = Area::get($city->pid);
        $adminModel = new Admin();
        $adminInfo  = $adminModel->where(['city' => $city->name, 'area' => $area->name])->find();
        if (!$adminInfo) {
            new Exception('该区域运营商不存在'.$city->name.'-'.$area->name);
        }
        $apply = self::info();
//         if ($apply['status'] == 1) {
//             new Exception('您的申请已通过，不需重复申请');
//         }

        $data['user_id']       = $user->id;
        $data['name']          = $name;
        $data['images']        = join(',', $images);
        $data['realname']      = $realname;
        $data['phone']         = $phone;
        $data['province_name'] = $province->name;
        $data['city_name']     = $city->name;
        $data['area_name']     = $area->name;
        $data['province_id']   = $province->id;
        $data['city_id']       = $city->id;
        $data['area_id']       = $area->id;
        $data['address']       = $address;
        $data['latitude']      = $latitude;
        $data['longitude']     = $longitude;
        $data['admin_id']      = $adminInfo->id;
        $data['salesperson_id'] = $salesperson_id;
        // $data['openhours'] = $openhours;
        // $data['openweeks'] = $openweeks;
        $data['status'] = 0;

        if ($apply) {
            $data['apply_num'] = $apply->apply_num + 1;
            $apply->save($data);
        } else {
            $data['apply_num'] = 1;
            $apply             = new self();
            $apply->save($data);
        }

        return $apply;
    }


    /* -------------------------- 访问器 ------------------------ */
    public function getStatusNameAttr($value, $data)
    {
        switch ($data['status']) {
            case -1:
                $status_name = '已拒绝';
                break;
            case 0:
                $status_name = '待审核';
                break;
            case 1:
                $status_name = '已通过';
                break;
            default :
                $status_name = '';
        }

        return $status_name;
    }


    public function getImagesAttr($value, $data)
    {
        $imagesArray = [];
        if (!empty($value)) {
            $imagesArray = explode(',', $value);
            foreach ($imagesArray as &$v) {
                $v = cdnurl($v, true);
            }
        }
        return $imagesArray;
    }


    public function getImagesOriginalAttr($value, $data)
    {
        $imagesArray = [];
        if (!empty($data['images'])) {
            $imagesArray = explode(',', $data['images']);
        }
        return $imagesArray;
    }

    /* -------------------------- 模型关联 ------------------------ */


    /* -------------------------- 模型关联 ------------------------ */
}
